﻿using System.Linq;
using System.ServiceModel;
using TfsGiggle.Core;
using NLog;

namespace TfsGiggle.Console
{
    class Program
    {
        private static Logger _logger = LogManager.GetCurrentClassLogger();

        static void Main(string[] args)
        {

            var address = new EndpointAddress("http://localhost:8086/Search");
            var binding = new WSHttpBinding(SecurityMode.None);
            var factory = new ChannelFactory<ISearch>(binding, address);
            var channel = factory.CreateChannel();

            System.Console.WriteLine("Enter a query");
            while (true)
            {
                var userQuery = System.Console.ReadLine();
                if (string.IsNullOrWhiteSpace(userQuery)) break;
                if (userQuery.ToLowerInvariant().Equals("status:"))
                {
                    int numberOfDocuments = channel.GetNumberOfDocuments();
                    System.Console.WriteLine("There are {0} documents in the index", numberOfDocuments);
                    for (int i = 0; i < numberOfDocuments; i++)
                    {
                        var result = channel.GetDocument(i);
                        if (string.IsNullOrWhiteSpace(result.Files[0].Name))
                        {
                            _logger.Warn("There's something fishy goin on");
                            continue;
                        }
                        System.Console.WriteLine("Name:{0}", result.Files[0].Name);
                        for (int j = 0; j < result.Changesets.Count(); j++)
                        {
                            System.Console.WriteLine("  Changeset:{0} {1} {2}", result.Changesets[j].Id, result.Changesets[j].Committer, result.Changesets[j].Comment);
                        }
                        System.Console.WriteLine();
                        for (int j = 0; j < result.Workitems.Count(); j++)
                        {
                            System.Console.WriteLine("  Workitem:{0} {1}", result.Workitems[j].Id, result.Workitems[j].Title);
                        }
                        System.Console.WriteLine();
                    }
                }
                else
                {
                    var results = channel.Query(userQuery);
                    if (results == null)
                    {
                        System.Console.WriteLine("No results");
                    }
                    else
                    {
                        System.Console.WriteLine("Files");
                        foreach (var result in results.Files)
                        {
                            System.Console.WriteLine("Name:{0}", result.Name);
                        }
                        System.Console.WriteLine("Changesets");
                        foreach (var result in results.Changesets)
                        {
                            System.Console.WriteLine("{0} {1} {2}", result.Id, result.Committer, result.Comment);
                        }
                        System.Console.WriteLine("Workitems");
                        foreach (var result in results.Workitems)
                        {
                            System.Console.WriteLine("{0} {1} {2}", result.Id, result.Title);
                        }
                    }
                }
            }
        }
    }
}
